package factory;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class BeanFactory {

	public static Object getBean(String beanName) {
		if(beanName == null || beanName.trim().equals("")) {
			return null;
		}
		 SAXReader reader = new SAXReader();
		 Document document = null;
		try {
			document = reader.read(BeanFactory.class.getResourceAsStream("/beans.xml"));
			
			Element root = document.getRootElement();
			Element node = (Element) root.selectSingleNode("bean[@id='"+beanName+"']");
			
			String className = node.attributeValue("class");
			return  Class.forName(className).newInstance();
			
		} catch (DocumentException | InstantiationException | IllegalAccessException | ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	
	
}
